﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    ViewData["Title"] = "产品信息管理";
}
<link href="~/css/site.css" rel="stylesheet" />
<link href="~/layui/css/layui.css" rel="stylesheet" />

<script src="~/layui/layui.js"></script>
<script src="~/js/site.js"></script>
<script src="~/lib/jquery/dist/jquery.js"></script>

<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">产品信息管理</h3>
                    <div class="card-tools">
                        <button type="button" class="btn btn-primary btn-sm" id="btnAdd">
                            <i class="layui-icon layui-icon-add-1"></i> 新增
                        </button>
                    </div>
                </div>
                <div class="card-body">

                    <!-- 表格区域 -->
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover" id="productTable">
                            <thead class="thead-light">
                                <tr>
                                    <th width="60">序号</th>
                                    <th>产品ID</th>
                                    <th>产品名称</th>
                                    <th>产品编码</th>
                                    <th>规格</th>
                                    <th>单位</th>
                                    <th>分类</th>
                                    <th>计划开始</th>
                                    <th>计划结束</th>
                                    <th>计划数量</th>

                                </tr>
                            </thead>
                            <tbody id="productTableBody">
                                <!-- 数据将通过JavaScript动态加载 -->
                            </tbody>
                        </table>
                    </div>

                    <!-- 加载状态 -->
                    <div id="loading" class="text-center" style="display: none;">
                        <i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop"></i> 加载中...
                    </div>

                    <!-- 无数据提示 -->
                    <div id="noData" class="text-center text-muted" style="display: none;">
                        暂无产品数据
                    </div>

                    <!-- 错误提示 -->
                    <div id="error" class="text-center text-danger" style="display: none;">
                        加载数据失败，请稍后重试
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(document).ready(function () {
        let productData = [];

        // 页面加载时获取数据
        loadProductData();



        // 新增按钮事件
        $('#btnAdd').click(function () {
            window.location.href = '/Home/AddProductItem';
        });

        // 加载产品数据
        function loadProductData() {
            showLoading();
            
            $.ajax({
                url: 'http://localhost:5025/api/Production/GetProductItem',
                type: 'GET',
                success: function (response) {
                    hideLoading();
                    if (response.code === 200) {
                        productData = response.data || [];
                        renderProductTable(productData);
                    } else {
                        showError('加载产品数据失败：' + response.msg);
                    }
                },
                error: function (xhr, status, error) {
                    hideLoading();
                    showError('请求失败：' + error);
                }
            });
        }



        // 渲染产品表格
        function renderProductTable(data) {
            const tbody = $('#productTableBody');
            tbody.empty();

            if (data.length === 0) {
                $('#noData').show();
                $('#productTable').hide();
                return;
            }

            $('#noData').hide();
            $('#productTable').show();
            
            data.forEach((product, index) => {
                const row = `
                    <tr>
                        <td>${index + 1}</td>
                        <td>${product.productId}</td>
                        <td>${product.productName || '-'}</td>
                        <td>${product.productCode || '-'}</td>
                        <td>${product.specification || '-'}</td>
                        <td>${product.unitName || '-'}</td>
                        <td>${product.classify || '-'}</td>
                        <td>${formatDateTime(product.planStart)}</td>
                        <td>${formatDateTime(product.planEnd)}</td>
                        <td>${product.planNum || 0}</td>
                    </tr>
                `;
                tbody.append(row);
            });
        }

        // 格式化日期时间
        function formatDateTime(dateTimeString) {
            if (!dateTimeString) return '-';
            const date = new Date(dateTimeString);
            return date.toLocaleString('zh-CN', { 
                year: 'numeric', 
                month: '2-digit', 
                day: '2-digit',
                hour: '2-digit',
                minute: '2-digit'
            });
        }

        // 显示加载状态
        function showLoading() {
            $('#loading').show();
            $('#productTable').hide();
            $('#noData').hide();
            $('#error').hide();
        }

        // 隐藏加载状态
        function hideLoading() {
            $('#loading').hide();
        }

        // 显示错误信息
        function showError(message) {
            $('#error').text(message).show();
            $('#productTable').hide();
            $('#noData').hide();
        }

        // 显示成功消息
        function showSuccess(message) {
            layer.msg(message, {
                icon: 1,
                time: 2000
            });
        }

        // 显示确认对话框
        function showConfirm(message, callback) {
            layer.confirm(message, {
                icon: 3,
                title: '确认操作',
                btn: ['确定', '取消']
            }, function (index) {
                layer.close(index);
                if (callback) callback();
            });
        }
    });


</script>

<style>
    .card {
        margin: 20px;
        box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
    }
    
    .card-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        background-color: #f8f9fa;
        border-bottom: 1px solid #dee2e6;
    }
    
    .card-tools {
        display: flex;
        align-items: center;
    }
    
    .table th {
        background-color: #f8f9fa;
        border-color: #dee2e6;
        font-weight: 500;
        vertical-align: middle;
    }
    
    .table td {
        vertical-align: middle;
    }
    

    
    .table-responsive {
        min-height: 400px;
    }
    
    #loading, #noData, #error {
        padding: 40px 0;
    }
    
    .layui-icon-loading {
        font-size: 24px;
        color: #007bff;
    }
</style>
